package com.gdzl_hadoop.common;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class PermissionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        String url = request.getRequestURI();
        System.out.println(url);
        if ((url.indexOf("/login.do") > 0) || (url.indexOf("/logout.do") > 0)) {
            System.out.println(true);
            return true;
        }

        System.out.println("-----PermissionInterceptor----");

        HttpSession session = request.getSession();
        if ((int) session.getAttribute("role") != 1) {
            if (url.indexOf("/data/getGeodateByXzqdm.do") > 0 || url.indexOf("/algorithm/FPG2.do") > 0) {
                response.getWriter().print("权限不足");
                return false;
            }
        }

        System.out.println(true);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub

    }

}
